<template>
  <el-form :rules="ruleInline" :model="form" ref="form">
    <el-form-item label="文章标题" prop="title" label-width="100px">
      <el-input @input="changeInput($event)" v-model="form.title"></el-input>
    </el-form-item>
    <el-form-item label="文章内容" label-width="100px">
      <!-- 使用 v-model 进行双向的数据绑定 -->
      <quill-editor
        class="qui"
        v-model="form.articleBody"
        :options="editorOption"
      ></quill-editor>
    </el-form-item>

    <el-form-item label="视频地址" label-width="100px"
      ><el-input
        placeholder="请输入视频地址"
        v-model="form.videoURL"
        @input="changeInput($event)"
      ></el-input>
    </el-form-item>
    <el-button @click="cancel">取 消</el-button>
    <el-button type="primary" @click="submitHandle">确 定</el-button>
  </el-form>
</template>

<script>
import { add, update } from '@/api/hmmm/articles'

export default {
  props: {
    dialogVisible: {
      type: Boolean
    },
    isEdit: {
      type: Boolean
    },
    row: {
      type: Object,
      default: () => {}
    }
  },

  data () {
    return {
      editorOption: {
        modules: {
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'],
            [{ list: 'ordered' }, { list: 'bullet' }],
            ['blockquote'],
            ['code-block', 'image', 'link']
          ],
          syntax: {
            highlight: (text) => {
              // 这里就是代码高亮需要配置的地方
            }
          }
        },
        theme: 'snow'
      },

      form: { title: '', articleBody: '', videoURL: '' },
      pid: '',
      ruleInline: {
        title: [
          { required: true, message: '文章标题不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    isEdit: {
      handler (newVal) {
        console.log(newVal, 9)
        if (newVal) {
          this.form = JSON.parse(JSON.stringify(this.row))
        }
      },
      immediate: true
    }
  },

  methods: {
    changeInput ($event) {
      this.$forceUpdate()
    },
    cancel () {
      this.$emit('update:dialogVisible', false)
    },

    submitHandle () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.isEdit ? this.doEdit() : this.doAdd()
        }
      })
    },

    async doAdd () {
      const res = await add(this.form)

      console.log(res, 34)
      this.success()
    },

    async doEdit () {
      const res = await update(this.form)
      console.log(res, 35)
      this.success()
    },
    success () {
      // 成功后需要做的操作 // 关闭对话框+刷新页面
      this.$emit('success')
    }
  }
}
</script>

<style scoped lang="scss">
::v-deep .ql-editor {
  min-height: 300px;
  min-width: 100px;
}
.qui {
  width: 620px;
}
</style>
